package org.eiahe.hr.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.OvertimeRule;
import org.eiahe.hr.attendance.domain.Shift;
import org.eiahe.hr.attendance.domain.bo.OvertimeRuleBo;
import org.eiahe.hr.attendance.domain.vo.OvertimeRuleVo;
import org.eiahe.hr.attendance.domain.vo.ShiftVo;
import org.eiahe.hr.attendance.mapper.OvertimeRuleMapper;
import org.eiahe.hr.attendance.mapper.ShiftMapper;
import org.eiahe.hr.attendance.service.IOvertimeRuleService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 加班规则服务实现
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class OvertimeRuleServiceImpl implements IOvertimeRuleService {

    private final OvertimeRuleMapper baseMapper;
    private final ShiftMapper shiftMapper;

    @Override
    public OvertimeRuleVo queryById(Long ruleId) {
        OvertimeRuleVo vo = baseMapper.selectVoById(ruleId);
        if (vo != null && vo.getShiftId() != null) {
            fillShiftInfo(vo);
        }
        return vo;
    }

    @Override
    public List<OvertimeRuleVo> queryList(OvertimeRuleBo bo) {
        LambdaQueryWrapper<OvertimeRule> lqw = buildQueryWrapper(bo);
        List<OvertimeRuleVo> list = baseMapper.selectVoList(lqw);
        list.forEach(this::fillShiftInfo);
        return list;
    }

    @Override
    public TableDataInfo<OvertimeRuleVo> queryPageList(OvertimeRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OvertimeRule> lqw = buildQueryWrapper(bo);
        Page<OvertimeRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(this::fillShiftInfo);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<OvertimeRule> buildQueryWrapper(OvertimeRuleBo bo) {
        LambdaQueryWrapper<OvertimeRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getShiftId() != null, OvertimeRule::getShiftId, bo.getShiftId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), OvertimeRule::getStatus, bo.getStatus());
        lqw.orderByAsc(OvertimeRule::getCreateTime);
        return lqw;
    }

    /**
     * 填充班次信息
     */
    private void fillShiftInfo(OvertimeRuleVo vo) {
        if (vo != null && vo.getShiftId() != null) {
            ShiftVo shiftVo = shiftMapper.selectVoById(vo.getShiftId());
            vo.setShiftVo(shiftVo);
            vo.setShiftName(shiftVo != null ? shiftVo.getShiftName() : null);
        }
    }

    @Override
    public Boolean insertByBo(OvertimeRuleBo bo) {
        OvertimeRule add = MapstructUtils.convert(bo, OvertimeRule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRuleId(add.getRuleId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(OvertimeRuleBo bo) {
        OvertimeRule update = MapstructUtils.convert(bo, OvertimeRule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void validEntityBeforeSave(OvertimeRule entity) {
        // 校验班次是否存在
        if (entity.getShiftId() != null) {
            Shift shift = shiftMapper.selectById(entity.getShiftId());
            if (shift == null) {
                throw new RuntimeException("班次不存在");
            }
        }

        // 校验该班次是否已存在加班规则
        if (entity.getShiftId() != null) {
            LambdaQueryWrapper<OvertimeRule> lqw = Wrappers.lambdaQuery();
            lqw.eq(OvertimeRule::getShiftId, entity.getShiftId());
            if (entity.getRuleId() != null) {
                lqw.ne(OvertimeRule::getRuleId, entity.getRuleId());
            }
            Long count = baseMapper.selectCount(lqw);
            if (count > 0) {
                throw new RuntimeException("该班次已存在加班规则");
            }
        }
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public OvertimeRuleVo queryByShiftId(Long shiftId) {
        if (shiftId == null) {
            return null;
        }
        LambdaQueryWrapper<OvertimeRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(OvertimeRule::getShiftId, shiftId);
        lqw.eq(OvertimeRule::getStatus, "0");
        lqw.last("LIMIT 1");
        OvertimeRuleVo vo = baseMapper.selectVoOne(lqw);
        if (vo != null) {
            fillShiftInfo(vo);
        }
        return vo;
    }
}
